<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>命名空间</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/namespace","wgTitle":"cpp/language/namespace","wgCurRevisionId":62626,"wgArticleId":2730,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with unreviewed CWG DR marker"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/namespace","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_namespace skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">命名空间</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="cpp/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 概览 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations">声明语法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E8.AF.B4.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明说明符序列</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html#.E5.A3.B0.E6.98.8E.E7.AC.A6" title="cpp/language/declarations"> 声明符</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 说明符 </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="cpp/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="inline.html" title="cpp/language/inline"><span class="t-lines"><span>inline</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="virtual.html" title="cpp/language/virtual"> virtual 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="explicit.html" title="cpp/language/explicit"> explicit 函数说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="friend.html" title="cpp/language/friend"><span class="t-lines"><span>friend</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constexpr.html" title="cpp/language/constexpr"><span class="t-lines"><span>constexpr</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="consteval.html" title="cpp/language/consteval"><span class="t-lines"><span>consteval</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="constinit.html" title="cpp/language/constinit"><span class="t-lines"><span>constinit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx20">(C++20)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储类说明符</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="class.html" title="cpp/language/class"><span class="t-lines"><span>class</span><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="cpp/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="cpp/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="decltype.html" title="cpp/language/decltype"><span class="t-lines"><span>decltype</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="auto.html" title="cpp/language/auto"><span class="t-lines"><span>auto</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="alignas.html" title="cpp/language/alignas"><span class="t-lines"><span>alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="cv.html" title="cpp/language/cv"><span class="t-lines"><span>const</span><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="elaborated_type_specifier.html" title="cpp/language/elaborated type specifier">详述类型说明符</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="attributes.html" title="cpp/language/attributes"> 属性</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
</table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> 声明符 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="reference.html" title="cpp/language/reference"> 引用</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="cpp/language/pointer"> 指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="cpp/language/array"> 数组</a> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 块声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 简单声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="structured_binding.html" title="cpp/language/structured binding">结构化绑定声明</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="type_alias.html" title="cpp/language/type alias"> 别名声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace_alias.html" title="cpp/language/namespace alias"> 命名空间别名定义</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="using_declaration.html" title="cpp/language/using declaration"> using 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace"> using 指令</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="static_assert.html" title="cpp/language/static assert"> static_assert 声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="asm.html" title="cpp/language/asm"> asm 声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="enum.html" title="cpp/language/enum"> 不可见 enum 声明</a><span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv-h2"><td colspan="5"> 其他声明 </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 命名空间定义</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function.html" title="cpp/language/function"> 函数声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html" title="cpp/language/class template"> 类模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="function_template.html" title="cpp/language/function template"> 函数模板声明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="class_template.html#.E6.98.BE.E5.BC.8F.E5.AE.9E.E4.BE.8B.E5.8C.96" title="cpp/language/class template"> 显式模板实例化</a><span class="t-mark-rev t-since-cxx11">(C++11)</span></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="template_specialization.html" title="cpp/language/template specialization"> 显式模板特化</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="language_linkage.html" title="cpp/language/language linkage"> 连接说明</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 属性声明</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="declarations.html" title="cpp/language/declarations"> 空声明</a> </td></tr>
</table></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>命名空间提供了在大项目中避免名字冲突的一种方法。
</p><p>声明于命名空间块内的符号被放入一个具名的作用域中，避免这些符号被误认为其他作用域中的同名符号。
</p><p>允许具有相同名字的多个命名空间块。这些块中的所有声明声明于该具名作用域。
</p>
<h3><span class="mw-headline" id=".E8.AF.AD.E6.B3.95">语法</span></h3>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>namespace</b></code> <span class="t-spar">命名空间名</span> { <span class="t-spar">声明序列</span> }
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>inline</b></code> <code><b>namespace</b></code> <span class="t-spar">命名空间名</span> { <span class="t-spar">声明序列</span> }
</td>
<td> (2)
</td>
<td> <span class="t-mark-rev t-since-cxx11">(C++11 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>namespace</b></code> { <span class="t-spar">声明序列</span> }
</td>
<td> (3)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">命名空间名</span><code><b>::</b></code><span class="t-spar">名字</span>
</td>
<td> (4)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>using</b></code> <code><b>namespace</b></code> <span class="t-spar">命名空间名</span><code><b>;</b></code>
</td>
<td> (5)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>using</b></code> <span class="t-spar">命名空间名</span><code><b>::</b></code><span class="t-spar">名字</span><code><b>;</b></code>
</td>
<td> (6)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>namespace</b></code> <span class="t-spar">名字</span> <code><b>=</b></code> <span class="t-spar">有限定命名空间</span> <code><b>;</b></code>
</td>
<td> (7)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>namespace</b></code> <span class="t-spar">命名空间名</span><code><b>::</b></code><code><b>inline</b></code><span class="t-mark-rev t-since-cxx20">(C++20 起)</span><span class="t-mark">(可选)</span> <span class="t-spar">名字</span> { <span class="t-spar">声明序列</span> }
</td>
<td> (8)
</td>
<td> <span class="t-mark-rev t-since-cxx17">(C++17 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<div class="t-li1"><span class="t-li">1)</span> 命名空间 <span class="t-spar">命名空间名</span> 的<a href="namespace.html#.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4" title="cpp/language/namespace">具名命名空间定义</a>。</div>
<div class="t-li1"><span class="t-li">2)</span> 命名空间 <span class="t-spar">命名空间名</span> 的<a href="namespace.html#.E5.86.85.E8.81.94.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4" title="cpp/language/namespace">内联命名空间定义</a>。<span class="t-spar">命名空间名</span> 内的声明将在其外围命名空间可见。</div>
<div class="t-li1"><span class="t-li">3)</span> <a href="namespace.html#.E6.97.A0.E5.90.8D.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4" title="cpp/language/namespace">无名命名空间定义</a>。其成员具有从声明点到翻译单元结尾的潜在作用域，并具有<a href="storage_duration.html" title="cpp/language/storage duration">内部连接</a>。</div>
<div class="t-li1"><span class="t-li">4)</span> 命名空间名（还有类名）可以出现在作用域解析运算符的左侧，作为<a href="lookup.html" title="cpp/language/lookup">有限定的名字查找</a>的一部分。</div>
<div class="t-li1"><span class="t-li">5)</span> <a href="namespace.html#using_.E6.8C.87.E4.BB.A4" title="cpp/language/namespace">using 指令</a>：从 using 指令之后到指令出现的作用域结尾为止，以对任何名字的无限定<a href="lookup.html" title="cpp/language/lookup">名字查找</a>的视点来说，来自 <span class="t-spar">命名空间名</span> 的任何名字均可见，如同它声明于同时含有该 using 指令和 <span class="t-spar">命名空间名</span> 两者的最接近外围命名空间作用域一样。</div>
<div class="t-li1"><span class="t-li">6)</span> <a href="namespace.html#using_.E5.A3.B0.E6.98.8E" title="cpp/language/namespace">using 声明</a>：令来自命名空间 <span class="t-spar">命名空间名</span> 的符号 <span class="t-spar">名字</span> 对<a href="lookup.html" title="cpp/language/lookup">无限定名字查找</a>可见，如同将它声明于包含该 using 声明的相同的类作用域、块作用域或命名空间之中一样。</div>
<div class="t-li1"><span class="t-li">7)</span> <span class="t-spar">命名空间别名定义（namespace-alias-definition）</span>：令 <span class="t-spar">名字</span> 成为另一命名空间的同义词：见<a href="namespace_alias.html" title="cpp/language/namespace alias">命名空间别名</a>。</div>
<div class="t-li1"><span class="t-li">8)</span> 嵌套命名空间定义：<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">namespace</span> A<span class="sy4">::</span><span class="me2">B</span><span class="sy4">::</span><span class="me2">C</span> <span class="br0">{</span> ... <span class="br0">}</span></span></span> 等价于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">namespace</span> A <span class="br0">{</span> <span class="kw1">namespace</span> B <span class="br0">{</span> <span class="kw1">namespace</span> C <span class="br0">{</span> ... <span class="br0">}</span> <span class="br0">}</span> <span class="br0">}</span></span></span>。 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx20"><td>
<p><span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">namespace</span> A<span class="sy4">::</span><span class="me2">B</span><span class="sy4">::</span><span class="kw1">inline</span> C <span class="br0">{</span> ... <span class="br0">}</span></span></span> 等价于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">namespace</span> A<span class="sy4">::</span><span class="me2">B</span> <span class="br0">{</span> <span class="kw1">inline</span> <span class="kw1">namespace</span> C <span class="br0">{</span> ... <span class="br0">}</span> <span class="br0">}</span></span></span>。<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">inline</span></span></span> 可出现于除首个以外的每个命名空间名之前：<span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">namespace</span> A<span class="sy4">::</span><span class="kw1">inline</span> B<span class="sy4">::</span><span class="me2">C</span> <span class="br0">{</span><span class="br0">}</span></span></span> 等价于 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw1">namespace</span> A <span class="br0">{</span> <span class="kw1">inline</span> <span class="kw1">namespace</span> B <span class="br0">{</span> <span class="kw1">namespace</span> C <span class="br0">{</span><span class="br0">}</span> <span class="br0">}</span> <span class="br0">}</span></span></span>。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td></tr>
</table></div>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<h4><span class="mw-headline" id=".E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4">命名空间</span></h4>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>inline</b></code><span class="t-mark">(可选)</span> <code><b>namespace</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <span class="t-spar">标识符</span> <code><b>{ </b></code> <span class="t-spar">命名空间体</span> <code><b>} </b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <code>inline</code>
</td>
<td> -
</td>
<td> 若存在，则令此命名空间为内联命名空间（见下文）。若<i>原初命名空间定义</i>不使用 <code>inline</code>，则不能出现在<i>扩展命名空间定义</i>上使用
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">attr</span><span class="t-mark-rev t-since-cxx17">(C++17)</span>
</td>
<td> -
</td>
<td> 任意数量的<a href="attributes.html" title="cpp/language/attributes">属性</a>的可选序列
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">标识符</span>
</td>
<td> -
</td>
<td> 要么是先前未使用过的标识符，该情况下这是<i>原初命名空间定义</i>，要么是命名空间名，该情况下这是<i>扩展命名空间定义</i><span class="t-rev-inl t-since-cxx17"><span>，要么是 :: 所分隔的外围命名空间说明符，以 <span class="t-spar">标识符</span> 结束，该情况下这是<i>嵌套命名空间定义</i></span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span>
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">命名空间体</span>
</td>
<td> -
</td>
<td> 任何种类（包含类、函数定义和嵌套命名空间等）的<a href="declarations.html" title="cpp/language/declarations">声明</a>的可能为空的序列
</td></tr></table>
<p>命名空间定义只允许在命名空间作用域，包括全局作用域中出现。
</p><p>为了重打开一个既存的命名空间（正式而言，作为<i>扩展命名空间定义</i>），对命名空间定义中所用的 <span class="t-spar">标识符</span> 的查找必须解析为一个命名空间名（而非命名空间别名），该名字被声明为某个外围命名空间或外围命名空间中的内联命名空间的成员。
</p><p><span class="t-spar">命名空间体</span> 定义了一个<a href="scope.html" title="cpp/language/scope">命名空间作用域</a>，这将影响<a href="lookup.html" title="cpp/language/lookup">名字查找</a>。
</p><p>出现于 <span class="t-spar">命名空间体</span>（包含嵌套命名空间定义）中的声明所引入的所有名字，都成为命名空间 <span class="t-spar">标识符</span> 的成员，无论此命名空间定义是原初命名空间定义（引入 <span class="t-spar">标识符</span> 者），还是扩展命名空间定义（“重打开”已定义命名空间者）。
</p><p>声明于命名空间体内的命名空间成员，可以在其外部用显式限定进行定义或再声明
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> Q <span class="br0">{</span>
  <span class="kw1">namespace</span> V <span class="br0">{</span> <span class="co1">// V 是 Q 的成员，且完全在 Q 内定义</span>
<span class="co1">// namespace Q::V { // C++17 中对上述二行的替代写法</span>
    <span class="kw1">class</span> C <span class="br0">{</span> <span class="kw4">void</span> m<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span> <span class="co1">// C 是 V 的成员且完全定义于 V 内</span>
                           <span class="co1">// C::m 仅声明</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// f 是 V 的成员，但只在此声明</span>
  <span class="br0">}</span>
  <span class="kw4">void</span> V<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span> <span class="co1">// V 的成员 f 的 V 外定义</span>
              <span class="co1">// f 的外围命名空间仍是全局命名空间、Q 与 Q::V</span>
  <span class="br0">{</span>
      <span class="kw4">extern</span> <span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 这声明 ::Q::V::h</span>
  <span class="br0">}</span>
  <span class="kw4">void</span> V<span class="sy4">::</span><span class="me2">C</span><span class="sy4">::</span><span class="me2">m</span><span class="br0">(</span><span class="br0">)</span> <span class="co1">// V::C::m 的命名空间（及类）外定义</span>
                 <span class="co1">// 外围命名空间是全局命名空间、Q 与 Q::V</span>
  <span class="br0">{</span>
  <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<p>命名空间外的定义和再声明仅在声明点后，仅在命名空间作用域，且仅在包围原命名空间的命名空间（包括全局命名空间）中允许出现<span class="t-rev-inl t-since-cxx14"><span>，而且它们必须使用限定标识语法</span> <span><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></span></span>。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> Q <span class="br0">{</span>
  <span class="kw1">namespace</span> V <span class="br0">{</span> <span class="co1">// V 的原初命名空间定义</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// Q::V::f 的声明</span>
  <span class="br0">}</span>
  <span class="kw4">void</span> V<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// OK</span>
  <span class="kw4">void</span> V<span class="sy4">::</span><span class="me2">g</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// 错误：g() 仍不是 V 的成员</span>
  <span class="kw1">namespace</span> V <span class="br0">{</span> <span class="co1">// V 的扩展命名空间定义</span>
    <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// Q::V::g 的声明</span>
  <span class="br0">}</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> R <span class="br0">{</span> <span class="co1">// 不是 Q 的外围命名空间</span>
   <span class="kw4">void</span> Q<span class="sy4">::</span><span class="me2">V</span><span class="sy4">::</span><span class="me2">g</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// 错误：不能在 R 内定义 Q::V::g</span>
<span class="br0">}</span>
<span class="kw4">void</span> Q<span class="sy4">::</span><span class="me2">V</span><span class="sy4">::</span><span class="me2">g</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span> <span class="co1">// OK：全局命名空间包围 Q</span></pre></div></div>
<p>在非局部类 X 中由<a href="friend.html" title="cpp/language/friend">友元</a>声明所引入的名字，会成为 X 的最内层外围命名空间的成员，但它们对于通常的<a href="lookup.html" title="cpp/language/lookup">名字查找</a>（<a href="unqualified_lookup.html" title="cpp/language/unqualified lookup">无限定</a>或<a href="qualified_lookup.html" title="cpp/language/qualified lookup">有限定</a>）不可见，除非在命名空间作用域提供与之匹配的声明，不论在类之前还是之后均可。这种名字可通过 <a href="adl.html" title="cpp/language/adl">ADL</a> 找到，其中同时考虑命名空间和类。
</p><p>这种友元声明，在确定名字是否与先前声明的名字冲突时，只考虑最内层外围命名空间。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw1">namespace</span> A <span class="br0">{</span>
  <span class="kw1">class</span> X <span class="br0">{</span>
    <span class="kw1">friend</span> <span class="kw4">void</span> f<span class="br0">(</span>X<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// A::f 是友元</span>
    <span class="kw1">class</span> Y <span class="br0">{</span>
        <span class="kw1">friend</span> <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// A::g 是友元</span>
        <span class="kw1">friend</span> <span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// A::h 是友元，与 ::h 不冲突</span>
    <span class="br0">}</span><span class="sy4">;</span>
  <span class="br0">}</span><span class="sy4">;</span>
  <span class="co1">// A::f、A::g 与 A::h 在命名空间作用域不可见</span>
  <span class="co1">// 虽然它们是命名空间 A 的成员</span>
  X x<span class="sy4">;</span>
  <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>  <span class="co1">// A::g 的定义</span>
     f<span class="br0">(</span>x<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// A::X::f 通过 ADL 找到</span>
  <span class="br0">}</span>
  <span class="kw4">void</span> f<span class="br0">(</span>X<span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>       <span class="co1">// A::f 的定义</span>
  <span class="kw4">void</span> h<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>     <span class="co1">// A::h 的定义</span>
  <span class="co1">// A::f 、 A::g 与 A::h 现在在命名空间作用域可见</span>
  <span class="co1">// 而且它们亦是 A::X 与 A::X::Y 的友元</span>
<span class="br0">}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx11"><td>
<h4> <span class="mw-headline" id=".E5.86.85.E8.81.94.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4">内联命名空间</span></h4>
<p>内联命名空间是在其<i>原初命名空间定义</i>中使用了可选的关键词 <code>inline</code> 的命名空间。
</p><p>许多情况下（列于下方），内联命名空间的成员都被当做如同它们是其外围命名空间的成员一样。这项性质是传递性的：若命名空间 N 包含内联命名空间 M，它又继而包含内联命名空间 O，则 O 的成员能按如同它们是 M 或 N 的成员一样使用。
</p>
<ul><li> 在外围命名空间中，隐含地插入了一条指名内联命名空间的 <i>using 指令</i>（类似于无名命名空间的隐式 using 指令）
</li><li> <a href="adl.html" title="cpp/language/adl">实参依赖查找</a>中，当将命名空间添加到关联命名空间集合时，亦添加其内联命名空间，而当添加内联命名空间到关联命名空间列表时，亦添加其外围命名空间。
</li><li> 内联命名空间的每个成员，都能按照如同它是外围命名空间的成员一样，进行部分特化、显式实例化或显式特化。
</li><li> 检验外围命名空间的有限定<a href="lookup.html" title="cpp/language/lookup">名字查找</a>，将包含来自其各个内联命名空间的名称，即使同一名称已存在于外围命名空间也是如此。
</li></ul>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="br0">{</span> <span class="co1">// C++14 中，std::literals 及其成员命名空间是内联的</span>
   <span class="kw1">using</span> <span class="kw1">namespace</span> std<span class="sy4">::</span><span class="me2">string_literals</span><span class="sy4">;</span> <span class="co1">// 令来自 std::literals::string_literals</span>
                                         <span class="co1">// 的 operator""s 可见</span>
   <span class="kw4">auto</span> str <span class="sy1">=</span> <span class="st0">"abc"</span>s<span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">{</span>
   <span class="kw1">using</span> <span class="kw1">namespace</span> std<span class="sy4">::</span><span class="me2">literals</span><span class="sy4">;</span> <span class="co1">// 令</span>
                                  <span class="co1">// std::literals::string_literals::operator""s 与</span>
                                  <span class="co1">// std::literals::chrono_literals::operator""s 均可见</span>
   <span class="kw4">auto</span> str <span class="sy1">=</span> <span class="st0">"abc"</span>s<span class="sy4">;</span>
   <span class="kw4">auto</span> min <span class="sy1">=</span> 60s<span class="sy4">;</span>
<span class="br0">}</span>
<span class="br0">{</span>
   <span class="kw1">using</span> std<span class="sy4">::</span><span class="me2">operator</span><span class="st0">""</span>s<span class="sy4">;</span> <span class="co1">// 令 std::literals::string_literals::operator""s 与</span>
                           <span class="co1">// std::literals::chrono_literals::operator""s 均可见</span>
   <span class="kw4">auto</span> str <span class="sy1">=</span> <span class="st0">"abc"</span>s<span class="sy4">;</span>
   <span class="kw4">auto</span> min <span class="sy1">=</span> 60s<span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p>注意：上述关于特化的规则允许建立库版本：库模板的不同实现可定义于不同内联命名空间，同时仍然允许用户通过主模板的显式特化来扩充父命名空间。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<h4><span class="mw-headline" id=".E6.97.A0.E5.90.8D.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4">无名命名空间</span></h4>
<p><i>无名命名空间定义</i>是具有下列形式的命名空间定义
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td class="t-sdsc-nopad"> <code><b>inline</b></code><span class="t-mark">(可选)</span> <code><b>namespace</b></code> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <code><b>{ </b></code> <span class="t-spar">命名空间体</span> <code><b>} </b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">attr</span><span class="t-mark-rev t-since-cxx17">(C++17)</span>
</td>
<td> -
</td>
<td> 任意数量的<a href="attributes.html" title="cpp/language/attributes">属性</a>的可选序列
</td></tr></table>
<p>此定义被当做一个拥有独有名字的命名空间定义，与当前作用域中指名此无名命名空间的一条 <i>using 指令</i>。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> <span class="br0">{</span>
    <span class="kw4">int</span> i<span class="sy4">;</span>  <span class="co1">// 定义 ::(独有)::i</span>
<span class="br0">}</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    i<span class="sy2">++</span><span class="sy4">;</span>  <span class="co1">// 自增 ::(独有)::i</span>
<span class="br0">}</span>
 
<span class="kw1">namespace</span> A <span class="br0">{</span>
    <span class="kw1">namespace</span> <span class="br0">{</span>
        <span class="kw4">int</span> i<span class="sy4">;</span> <span class="co1">// A::(独有)::i</span>
        <span class="kw4">int</span> j<span class="sy4">;</span> <span class="co1">// A::(独有)::j</span>
    <span class="br0">}</span>
    <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span> i<span class="sy2">++</span><span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// A::(独有)::i++</span>
<span class="br0">}</span>
 
<span class="kw1">using</span> <span class="kw1">namespace</span> A<span class="sy4">;</span> <span class="co1">// 从 A 引入所有名称到全局命名空间</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    i<span class="sy2">++</span><span class="sy4">;</span>    <span class="co1">// 错误：::(独有)::i 与 ::A::(独有)::i 均在作用域中</span>
    A<span class="sy4">::</span><span class="me2">i</span><span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// OK：自增 A::(独有)::i</span>
    j<span class="sy2">++</span><span class="sy4">;</span>    <span class="co1">// OK：自增 A::(独有)::j</span>
<span class="br0">}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx11"><td>
<p>虽然无名命名空间中的名字可以声明为具有外部连接，但绝对无法从其他翻译单元访问它们，因为其命名空间名是独有的。
</p>
</td>
<td><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></td></tr>
<tr class="t-rev t-since-cxx11"><td>
<p>无名命名空间以及所有直接或间接在无名命名空间内声明的命名空间，都具有<a href="storage_duration.html#.E8.BF.9E.E6.8E.A5" title="cpp/language/storage duration">内部连接</a>，这表示声明于无名命名空间内的任何名字都具有内部连接。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table>
<h4><span class="mw-headline" id="using_.E5.A3.B0.E6.98.8E">using 声明</span></h4>
<p>引入定义于别处的名称到此 using 声明出现的声明性区域。
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>using</b></code> <code><b>typename</b></code><span class="t-mark">(可选)</span> <span class="t-spar">嵌套名说明符</span> <span class="t-spar">无限定标识</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td> <span class="t-mark-rev t-until-cxx17">(C++17 前)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <code><b>using</b></code> <span class="t-spar">声明符列表</span> <code><b>;</b></code>
</td>
<td class="t-sdsc-nopad">
</td>
<td> <span class="t-mark-rev t-since-cxx17">(C++17 起)</span>
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">嵌套名说明符</span>
</td>
<td> -
</td>
<td> 名字与作用域解析运算符 <code>::</code> 的序列，以作用域解析运算符结束。单个 <code>::</code> 代表全局命名空间。
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">无限定标识</span>
</td>
<td> -
</td>
<td> <a href="name.html" title="cpp/language/identifiers">标识表达式</a>
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">typename</span>
</td>
<td> -
</td>
<td> 当 using 声明向类模板引入基类的成员类型时，关键词 <code>typename</code> 可在必要时用于解析<a href="dependent_name.html" title="cpp/language/dependent name">待决名</a>
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">声明符列表</span>
</td>
<td> -
</td>
<td> 一或多个形式为 <code><b>typename</b></code><span class="t-mark">(可选)</span> <span class="t-spar">嵌套名说明符</span> <span class="t-spar">无限定标识</span> 的声明符的逗号分隔列表。声明符可以后随省略号以指示<a href="parameter_pack.html" title="cpp/language/parameter pack">包展开</a>，但这种形式只在<a href="using_declaration.html" title="cpp/language/using declaration">派生类定义</a>中有意义
</td></tr></table>
<p>using 声明可用于将命名空间的成员引入到其他命名空间和块作用域中，或将基类的成员引入到派生类定义中。
</p>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx17"><td>
<p>拥有多于一个 using 声明符的 using 声明，等价于对应的单个 using 声明符的 using 声明的序列。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></td></tr>
</table>
<p>对于在派生类定义中的用法，见 <a href="using_declaration.html" title="cpp/language/using declaration">using 声明</a>。
</p><p>由 using 声明引入到命名空间作用域的名字，可以同任何其他名字一样使用，包含从其他作用域进行的有限定查找：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="kw1">namespace</span> A <span class="br0">{</span>
    <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> X <span class="br0">{</span>
    <span class="kw1">using</span> <span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span> <span class="co1">// 全局 f 现在作为 ::X::f 可见</span>
    <span class="kw1">using</span> A<span class="sy4">::</span><span class="me2">g</span><span class="sy4">;</span> <span class="co1">// A::g 现在作为 ::X::g 可见</span>
    <span class="kw1">using</span> A<span class="sy4">::</span><span class="me2">g</span>, A<span class="sy4">::</span><span class="me2">g</span><span class="sy4">;</span> <span class="co1">// (C++17) OK：命名空间作用域允许双重声明</span>
<span class="br0">}</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    X<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 ::f</span>
    X<span class="sy4">::</span><span class="me2">g</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 A::g</span>
<span class="br0">}</span></pre></div></div>
<p>在用 using 声明从命名空间采取成员后，若该命名空间被扩充并引入了同名的额外声明，则这些额外声明不会通过该 using 声明变为可见（与 using 指令相反）。一个例外是 using 声明指名类模板时：后面引入的部分特化实际上是可见的，因为其<a href="lookup.html" title="cpp/language/lookup">查找</a>是通过主模板达成的。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> A <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">using</span> A<span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span> <span class="co1">// ::f 现在是 A::f(int) 的同义词</span>
 
<span class="kw1">namespace</span> A <span class="br0">{</span> <span class="co1">// 命名空间扩展</span>
   <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 不更改 ::f 的含义</span>
<span class="br0">}</span>
<span class="kw4">void</span> foo<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    f<span class="br0">(</span><span class="st0">'a'</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 f(int)，即使 f(char) 存在。</span>
<span class="br0">}</span> 
<span class="kw4">void</span> bar<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
   <span class="kw1">using</span> A<span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span> <span class="co1">// 此 f 是 A::f(int) 与 A::f(char) 的同义词</span>
   f<span class="br0">(</span><span class="st0">'a'</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 f(char)</span>
<span class="br0">}</span></pre></div></div>
<p>using 声明不能指名 <a href="templates.html#.E6.A8.A1.E6.9D.BF.E6.A0.87.E8.AF.86" title="cpp/language/templates">模板标识</a>、命名空间或有作用域枚举项。using 声明中的每个声明符引入一个且只有一个名字，例如<a href="enum.html" title="cpp/language/enum">枚举</a> 的 using 声明不引入其任何枚举项。
</p><p>针对相同名字的常规声明的所有制约，隐藏和重载规则，均适用于 using 声明：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> A <span class="br0">{</span>
    <span class="kw4">int</span> x<span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> B <span class="br0">{</span>
    <span class="kw4">int</span> i<span class="sy4">;</span>
    <span class="kw1">struct</span> g <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
    <span class="kw1">struct</span> x <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> g<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：函数名 g 隐藏类 g</span>
<span class="br0">}</span>
<span class="kw4">void</span> func<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">int</span> i<span class="sy4">;</span>
    <span class="kw1">using</span> B<span class="sy4">::</span><span class="me2">i</span><span class="sy4">;</span> <span class="co1">// 错误：两次声明 i</span>
 
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw1">using</span> B<span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span> <span class="co1">// OK：f(char)、f(int)、f(double) 是重载</span>
    f<span class="br0">(</span><span class="nu16">3.5</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 调用 B::f(double)</span>
 
    <span class="kw1">using</span> B<span class="sy4">::</span><span class="me2">g</span><span class="sy4">;</span>
    g<span class="br0">(</span><span class="st0">'a'</span><span class="br0">)</span><span class="sy4">;</span>      <span class="co1">// 调用 B::g(char)</span>
    <span class="kw1">struct</span> g g1<span class="sy4">;</span> <span class="co1">// 声明 g1 拥有类型 struct B::g</span>
 
    <span class="kw1">using</span> B<span class="sy4">::</span><span class="me2">x</span><span class="sy4">;</span>
    <span class="kw1">using</span> A<span class="sy4">::</span><span class="me2">x</span><span class="sy4">;</span>  <span class="co1">// OK ：隐藏 B::x</span>
    x <span class="sy1">=</span> <span class="nu0">99</span><span class="sy4">;</span>      <span class="co1">// 赋值给 A::x</span>
    <span class="kw1">struct</span> x x1<span class="sy4">;</span> <span class="co1">// 声明 x1 拥有类型 struct B::x</span>
<span class="br0">}</span></pre></div></div>
<p>若用 using 声明引入函数，则声明拥有相同名字和形参列表的函数是非良构的（除非是同一函数的声明）。若用 using 声明引入函数模板，则声明拥有相同名字、形参类型列表、返回类型及模板形参列表的函数模板是非良构的。两个 using 声明可以引入拥有相同名字和形参列表的函数，但若试图调用该函数，则程序非良构。 
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> B <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> C <span class="br0">{</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">double</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw1">using</span> B<span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span> <span class="co1">// 引入 B::f(int) 、 B::f(double)</span>
    <span class="kw1">using</span> C<span class="sy4">::</span><span class="me2">f</span><span class="sy4">;</span> <span class="co1">// 引入 C::f(int) 、 C::f(double) 及 C::f(char)</span>
    f<span class="br0">(</span><span class="st0">'h'</span><span class="br0">)</span><span class="sy4">;</span>      <span class="co1">// 调用 C::f(char)</span>
    f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>        <span class="co1">// 错误：B::f(int) 或 C::f(int) ？</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：f(int) 与 C::f(int) 及 B::f(int) 冲突</span>
<span class="br0">}</span></pre></div></div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-cxx14"><td>
<p>若某个实体被声明，但未在某内层命名空间中定义，然后在外层命名空间中通过 using 声明予以声明，然后在外层命名空间中再出现拥有相同非限定名的定义，则该定义是外层命名空间的成员，且与 using 声明冲突：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> X <span class="br0">{</span>
  <span class="kw1">namespace</span> M <span class="br0">{</span>
     <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 声明，但不定义 X::M::g()</span>
  <span class="br0">}</span>
  <span class="kw1">using</span> M<span class="sy4">::</span><span class="me2">g</span><span class="sy4">;</span>
  <span class="kw4">void</span> g<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>   <span class="co1">// 错误：试图声明与 X::M::g() 冲突的 X::g</span>
<span class="br0">}</span></pre></div></div>
<p>更一般地，出现于任何命名空间作用域中并用无限定标识符引入名字的声明，始终向它所在的命名空间中引入一个成员，而并非向任何其他命名空间引入。例外情况是对定义于内联命名空间的主模板进行的显式实例化和显式特化：因为它们不引入新名字，它们在外围命名空间中可以使用无限定标识。
</p>
</td>
<td><span class="t-mark-rev t-since-cxx14">(C++14 起)</span></td></tr>
</table>
<h4><span class="mw-headline" id="using_.E6.8C.87.E4.BB.A4">using 指令</span></h4>
<p><i>using 指令</i>是拥有下列语法的<a href="declarations.html" title="cpp/language/declarations">块声明</a>：
</p>
<table class="t-sdsc-begin">

<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr>
<tr class="t-sdsc">
<td> <span class="t-spar">attr</span><span class="t-mark">(可选)</span> <code><b>using</b></code> <code><b>namespace</b></code> <span class="t-spar">嵌套名说明符</span><span class="t-mark">(可选)</span> <span class="t-spar">命名空间名</span> <code><b>;</b></code>
</td>
<td> (1)
</td>
<td class="t-sdsc-nopad">
</td></tr>



<tr>
<td colspan="10" class="t-sdsc-sep">
</td></tr></table>
<table class="t-par-begin">


<tr class="t-par">
<td>  <span class="t-spar">attr</span><span class="t-mark-rev t-since-cxx11">(C++11)</span>
</td>
<td> -
</td>
<td> 应用到此 using 指令的任意数量的<a href="attributes.html" title="cpp/language/attributes">属性</a>
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">嵌套名说明符</span>
</td>
<td> -
</td>
<td> 名字与作用域解析运算符 <code>::</code> 的序列，以作用域解析运算符结束。单个 <code>::</code> 代表全局命名空间。
</td></tr>
<tr class="t-par">
<td>  <span class="t-spar">命名空间名</span>
</td>
<td> -
</td>
<td> 命名空间名。查找此名时，<a href="lookup.html" title="cpp/language/lookup">查找</a>只考虑命名空间声明
</td></tr></table>
<p>using 指令仅在命名空间<a href="scope.html" title="cpp/language/scope">作用域</a>和块作用域中允许出现。从某个 using 指令之后到该指令出现的作用域结尾为止，以任何名字的无限定<a href="lookup.html" title="cpp/language/lookup">名字查找</a>的视点，来自 <span class="t-spar">命名空间名</span> 的每个名字均可见，如同它声明于同时包含该 using 指令和 <span class="t-spar">命名空间名</span> 两者的最接近外围命名空间一样。
</p><p>using 指令不向其所出现的声明性区域添加任何名字（不同于 using 声明），因而并不妨碍再声明相同的名字。
</p><p>using 指令对于<a href="lookup.html" title="cpp/language/lookup">无限定查找</a>是传递性的：若作用域包含指名 <span class="t-spar">命名空间名</span> 的 using 指令，而它自身包含对某 <span class="t-spar">命名空间名-2</span> 的 using 指令，则效果如同第二个命名空间中的 using 指令出现在第一个之中一样。这些传递性命名空间的出现顺序并不影响名字查找。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> A <span class="br0">{</span>
    <span class="kw4">int</span> i<span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> B <span class="br0">{</span>
    <span class="kw4">int</span> i<span class="sy4">;</span>
    <span class="kw4">int</span> j<span class="sy4">;</span>
    <span class="kw1">namespace</span> C <span class="br0">{</span>
        <span class="kw1">namespace</span> D <span class="br0">{</span>
            <span class="kw1">using</span> <span class="kw1">namespace</span> A<span class="sy4">;</span> <span class="co1">// 注入所有来自 A 的名称到全局命名空间</span>
            <span class="kw4">int</span> j<span class="sy4">;</span>
            <span class="kw4">int</span> k<span class="sy4">;</span>
            <span class="kw4">int</span> a <span class="sy1">=</span> i<span class="sy4">;</span> <span class="co1">// i 是 B::i，因为 B::i 隐藏 A::i</span>
        <span class="br0">}</span>
        <span class="kw1">using</span> <span class="kw1">namespace</span> D<span class="sy4">;</span> <span class="co1">// 注入来自 D 的名称到 C</span>
                           <span class="co1">// 注入来自 A 的名称到全局命名空间</span>
        <span class="kw4">int</span> k <span class="sy1">=</span> <span class="nu0">89</span><span class="sy4">;</span> <span class="co1">// 声明与用 using 引入者等同的名称 OK</span>
        <span class="kw4">int</span> l <span class="sy1">=</span> k<span class="sy4">;</span>  <span class="co1">// 歧义：C::k 或 D::k</span>
        <span class="kw4">int</span> m <span class="sy1">=</span> i<span class="sy4">;</span>  <span class="co1">// OK：B::i 隐藏 A::i</span>
        <span class="kw4">int</span> n <span class="sy1">=</span> j<span class="sy4">;</span>  <span class="co1">// OK：D::j 隐藏 B::j</span>
    <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<p>在使用 using 指令指名某命名空间后，若该命名空间被扩充并向其添加了额外的成员和/或 using 指令，则这些额外成员和额外的命名空间通过该 using 指令可见（与 using 声明相反）
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> D <span class="br0">{</span>
   <span class="kw4">int</span> d1<span class="sy4">;</span>
   <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">using</span> <span class="kw1">namespace</span> D<span class="sy4">;</span> <span class="co1">// 引入 D::d1、D::f、D::d2、D::f，</span>
                   <span class="co1">// E::e 及 E::f 到全局命名空间！</span>
 
<span class="kw4">int</span> d1<span class="sy4">;</span> <span class="co1">// OK：声明时与 D::d1 不冲突</span>
<span class="kw1">namespace</span> E <span class="br0">{</span>
    <span class="kw4">int</span> e<span class="sy4">;</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> D <span class="br0">{</span> <span class="co1">// 命名空间扩展</span>
    <span class="kw4">int</span> d2<span class="sy4">;</span>
    <span class="kw1">using</span> <span class="kw1">namespace</span> E<span class="sy4">;</span> <span class="co1">// 传递性 using 指令</span>
    <span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    d1<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// 错误：歧义：::d1 或 D::d1？</span>
    <span class="sy4">::</span><span class="me2">d1</span><span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// OK</span>
    D<span class="sy4">::</span><span class="me2">d1</span><span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// OK</span>
    d2<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// OK，d2 是 D::d2</span>
    e<span class="sy2">++</span><span class="sy4">;</span> <span class="co1">// OK：e 是 E::e，因为传递性 using</span>
    f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：歧义：D::f(int) 或 E::f(int)？</span>
    f<span class="br0">(</span><span class="st0">'a'</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK：仅有的 f(char) 是 D::f(char)</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">注解</span></h3>
<p>在任何命名空间作用域中的 using 指令 <code>using namespace std;</code>，将命名空间 <code>std</code> 中的所有名字都引入到全局命名空间中（因为全局命名空间是同时包含 <code>std</code> 和任何用户声明命名空间的最近命名空间），这可能导致不合预期的名字冲突。通常认为，在头文件的文件作用域中采用它或其他的 using 指令是不良的实践。
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><p>此例展示如何用命名空间创建已命名于 <code>std</code> 命名空间的类。</p><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;vector&gt;</span>
 
<span class="kw1">namespace</span> vec <span class="br0">{</span>
 
    <span class="kw1">template</span><span class="sy1">&lt;</span> <span class="kw1">typename</span> T <span class="sy1">&gt;</span>
    <span class="kw1">class</span> vector <span class="br0">{</span>
        <span class="co1">// ...</span>
    <span class="br0">}</span><span class="sy4">;</span>
 
<span class="br0">}</span> <span class="co1">// of vec</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../container/vector.html"><span class="kw1269">std::<span class="me2">vector</span></span></a><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v1<span class="sy4">;</span> <span class="co1">// 标准 vector。</span>
    vec<span class="sy4">::</span><span class="me2">vector</span><span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v2<span class="sy4">;</span> <span class="co1">// 用户定义 vector。</span>
 
    v1 <span class="sy1">=</span> v2<span class="sy4">;</span> <span class="co1">// 错误：v1 与 v2 是不同类型的对象。</span>
 
    <span class="br0">{</span>
        <span class="kw1">using</span> <span class="kw1">namespace</span> std<span class="sy4">;</span>
        vector<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v3<span class="sy4">;</span> <span class="co1">// 同 std::vector</span>
        v1 <span class="sy1">=</span> v3<span class="sy4">;</span> <span class="co1">// OK</span>
    <span class="br0">}</span>
 
    <span class="br0">{</span>
        <span class="kw1">using</span> vec<span class="sy4">::</span><span class="me2">vector</span><span class="sy4">;</span>
        vector<span class="sy1">&lt;</span><span class="kw4">int</span><span class="sy1">&gt;</span> v4<span class="sy4">;</span> <span class="co1">// 同 vec::vector</span>
        v2 <span class="sy1">=</span> v4<span class="sy4">;</span> <span class="co1">// OK</span>
    <span class="br0">}</span>
 
    <span class="kw1">return</span> <span class="nu0">0</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<p><br> 
</p>
</div>
<h3><span class="mw-headline" id=".E7.BC.BA.E9.99.B7.E6.8A.A5.E5.91.8A">缺陷报告</span></h3>
<p>下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
</p>
<table class="dsctable" style="font-size:0.8em">
<tr>
<th> DR
</th>
<th> 应用于
</th>
<th> 出版时的行为
</th>
<th> 正确行为
</th></tr>
<tr>
<td> <a rel="nofollow" class="external text" href="https://wg21.cmeerw.net/cwg/issue1838">CWG 1838</a>
</td>
<td> C++14
</td>
<td> 外层命名空间的非限定定义，曾能定义声明但不定义于另一命名空间的实体，并用 using 拉入
</td>
<td> 非限定定义始终指涉其命名空间
</td></tr></table>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

<tr class="t-dsc">
<td> <a href="namespace_alias.html" title="cpp/language/namespace alias">命名空间别名</a>
</td>
<td> 为既存命名空间创建一个别名</td></tr>
</table>

<!-- 
NewPP limit report
Preprocessor visited node count: 4806/1000000
Preprocessor generated node count: 11665/1000000
Post‐expand include size: 101102/2097152 bytes
Template argument size: 34868/2097152 bytes
Highest expansion depth: 15/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:2730-0!*!0!!zh!*!zh!* and timestamp 20200223143944 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/namespace&amp;oldid=62626">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/namespace&amp;oldid=62626</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/namespace">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2019年9月5日 (星期四) 17:52。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过19,664次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 2.327 secs. -->
	</body>
<!-- Cached 20200223143944 -->
</html>